define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        _queryString: '',
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'zzyy/roomclose/index' + Controller._queryString,
                    add_url: 'zzyy/roomclose/add' + Controller._queryString,
                    edit_url: 'zzyy/roomclose/edit',
                    del_url: 'zzyy/roomclose/del',
                    multi_url: 'zzyy/roomclose/multi',
                    import_url: 'zzyy/roomclose/import',
                    table: 'zzyy_roomclose',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='store_id']", form).addClass("selectpage").data("source", "zzyy/store/ajaxgetstorewithquanoption").data("primaryKey", "value").data("field", "name");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'store_id', title: __('Store_id'), operate: 'LIKE', visible: false},
                        {field: 'store.store_name', title: __('Store_id'), operate: false, formatter: Controller.api.showstore},
                        {field: 'room.room_name', title: __('Room_id'), operate: false, formatter: Controller.api.showroom},
                        {field: 'roomclose_starttime', title: __('Roomclose_starttime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'roomclose_endtime', title: __('Roomclose_endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                searchFormVisible: true,
                search: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            showstore: function (e) {
                if (null == e) {
                    return '全部门店'
                }
                return e
            },
            showroom: function (e) {
                if (null == e) {
                    return '全部教室'
                }
                return e
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            queryString: function () {
                return location.search.replace("dialog=1", "").split('&').filter(function (item) {
                    return !!item;
                }).join("&");
            }
        }
    };
    Controller._queryString = Controller.api.queryString();
    return Controller;
});